package com.wyh.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wyh.blog.aspect.BlogInfoException;
import com.wyh.blog.constant.consist.Constants;
import com.wyh.blog.mapper.BlogInfoMapper;
import com.wyh.blog.model.entity.BlogInfo;
import com.wyh.blog.model.vo.request.AddBlogRequest;
import com.wyh.blog.model.vo.request.UpdateRequest;
import com.wyh.blog.model.vo.response.BlogDetailVo;
import com.wyh.blog.model.vo.response.BlogInfoVo;
import com.wyh.blog.service.intf.BlogInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BlogInfoServiceImpl implements BlogInfoService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoVo> getList(){
        QueryWrapper<BlogInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BlogInfo::getDeleteFlag,Constants.NORMAL);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(wrapper);
        List<BlogInfoVo> blogInfoVos = blogInfos.stream().map(blogInfo -> {
            BlogInfoVo blogInfoVo = new BlogInfoVo();
            BeanUtils.copyProperties(blogInfo, blogInfoVo);
            return blogInfoVo;
        }).collect(Collectors.toList());
        return blogInfoVos;
    }

    @Override
    public BlogDetailVo getBlogDetail(Integer id) {
        LambdaQueryWrapper<BlogInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogInfo::getId, id).eq(BlogInfo::getDeleteFlag,0);
        BlogInfo blogInfo = blogInfoMapper.selectOne(wrapper);
        if (blogInfo == null) return null;
        BlogDetailVo blogDetailVo = new BlogDetailVo();
        BeanUtils.copyProperties(blogInfo,blogDetailVo);
        return blogDetailVo;
    }

    @Override
    public boolean add(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest,blogInfo);
        try {
            int total = blogInfoMapper.insert(blogInfo);
            return total == 1;
        }catch (Exception e){
            log.error("发布博客失败，userId: {}, title: {}", addBlogRequest.getUserId(),addBlogRequest.getTitle());
            throw new BlogInfoException("发布博客失败");
        }
    }

    @Override
    public boolean update(UpdateRequest request) {
        try {
            BlogInfo blogInfo = new BlogInfo();
            BeanUtils.copyProperties(request, blogInfo);
            int i = blogInfoMapper.updateById(blogInfo);
            return i == 1;
        }catch (Exception e){
            log.error("更新博客失败, id: {}", request.getId());
            throw new BlogInfoException("更新博客失败");
        }
    }

    @Override
    public boolean delete(Integer id) {
        try {
            UpdateWrapper<BlogInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(BlogInfo::getId,id)
                    .set(BlogInfo::getDeleteFlag, Constants.DELETE);
            int i = blogInfoMapper.update(updateWrapper);
            return i==1;
        }catch (Exception e){
            log.error("删除博客失败，id: {}", id);
            throw new BlogInfoException("删除博客失败");
        }
    }
}
